package com.gwd.task.services.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.gwd.task.componts.Cache;
import com.gwd.task.entity.EventsProcessEntity;
import com.gwd.task.model.EventsProcessDTO;
import com.gwd.task.repository.EventsProcessRepository;
import com.gwd.task.services.EventsProcessServer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhiyongming
 * @date 2020/12/16 14:39
 */
@Service
public class EventsProcessServerImpl implements EventsProcessServer{

    @Resource
    private Cache cache;
    @Resource
    private EventsProcessRepository eventsProcessRepository;

    @Override
    public List<EventsProcessDTO> findAll() {
        return eventsProcessRepository.findAll().stream().map(EventsProcessDTO::new).collect(Collectors.toList());
    }

    @Override
    public List<EventsProcessDTO> findByEventId(Long id) {
        return eventsProcessRepository.findByEventId(id).stream().map(EventsProcessDTO::new).collect(Collectors.toList());
    }

    @Override
    public List<EventsProcessDTO> findByEventIdList(List<Long> idList) {
        List<EventsProcessDTO> dtoList =
            eventsProcessRepository.findByEventIdIn(idList).stream().map(EventsProcessDTO::new).collect(Collectors.toList());
        cache.matchUserName(dtoList, "getOperationUser", "setOperationUserName");
        return dtoList;
    }

    @Override
    public void save(EventsProcessDTO dto) {
        EventsProcessEntity entity = new EventsProcessEntity();
        BeanUtil.copyProperties(dto, entity, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        eventsProcessRepository.save(entity);
    }
}
